<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="partyId">
        <el-select v-model="queryParams.partyId" placeholder="请选择申请人" clearable filterable>
          <el-option v-for="dict in partyIdOptions" :key="dict.partyId" :label="dict.firstName" :value="dict.partyId" />
        </el-select>
      </el-form-item>
      <el-form-item label="请假类型" prop="vacationTypeId">
        <el-select v-model="queryParams.vacationTypeId" placeholder="请选择请假类型" clearable>
          <el-option v-for="dict in vacationTypeIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini">
          <router-link :to="'/workflow/approve/vacation/apply'" class="link-type">
            <span>请假记录申请</span>
          </router-link>
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:vacation_record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vacation_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" width="80" prop="vacationRecordId" />
      <el-table-column label="申请人" align="center" prop="partyId" width="100" :formatter="partyIdFormat" />
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.applyDate, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="vacationTypeId" width="100" :formatter="vacationTypeIdFormat" />
      <el-table-column label="请假总工时" align="center" width="100" prop="totalWorkTime" />
      <el-table-column label="请假事由" align="center" prop="description" />

      <el-table-column label="流程状态" align="center" prop="flowStatus" width="100" :formatter="flowStatusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:vacation_record:detail']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改请假记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled="detailFlag">
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请时间" prop="applyDate">
              <span>{{ form.applyDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" prop="partyId">
              <span>{{ partyIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" prop="vacationTypeId">
              <span>{{ vacationTypeIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="fromdate">
              <span>{{ form.fromdate }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="从几时开始" prop="fromDateHour">
              <span>{{ fromDateHourFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="thrudate">
              <span>{{ form.thrudate }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="从几时结束" prop="thruDateHour">
              <span>{{ thruDateHourFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假总工时" prop="totalWorkTime">
              <span>{{ form.totalWorkTime }} 小时</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="请假事由" prop="description">
              <span>{{ form.description }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务状态" prop="statusId">
              <span>{{ flowStatusFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="附件" prop="affixFilePath">
              <image-preview :src="form.affixFilePath ? form.affixFilePath : ''" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVacation_record,
  getVacation_record,
  delVacation_record,
  addVacation_record,
  updateVacation_record,
} from "@/api/ehr/vacation_record";
// person enum api
import { getPersonEnum } from "@/api/ehr/person";
export default {
  name: "Vacation_record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假记录表格数据
      vacation_recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 人员字典
      personOptions: [],
      // 申请人ID业务字典
      partyIdOptions: [],
      // 请假类型业务字典
      vacationTypeIdOptions: [],
      // 从几时开始业务字典
      fromDateHourOptions: [],
      // 从几时结束业务字典
      thruDateHourOptions: [],
      // 审批结论业务字典
      approvalResultOptions: [],
      // 流程状态业务字典
      flowStatusOptions: [],
      // 审批人业务字典
      handlerOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        partyId: null,
        vacationTypeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partyId: [
          { required: true, message: "申请人ID不能为空", trigger: "change" },
        ],
        vacationTypeId: [
          { required: true, message: "请假类型不能为空", trigger: "change" },
        ],
        fromdate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        fromDateHour: [
          { required: true, message: "从几时开始不能为空", trigger: "change" },
        ],
        thrudate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        thruDateHour: [
          { required: true, message: "从几时结束不能为空", trigger: "change" },
        ],
        approvalResult: [
          { required: true, message: "审批结论不能为空", trigger: "blur" },
        ],
        handler: [
          { required: true, message: "审批人不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    // 人员字典
    getPersonEnum().then((response) => {
      this.personOptions = response.data;
    });
    // 员工ID业务字典
    this.getEntityDicts("person:partyId:firstName").then((response) => {
      this.partyIdOptions = response.data;
    });
    // 请假类型业务字典
    this.getEnumDicts("VACATION_TYPE").then((response) => {
      this.vacationTypeIdOptions = response.data;
    });
    // 从几时开始业务字典
    this.getEnumDicts("VACATION_TIME").then((response) => {
      this.fromDateHourOptions = response.data;
    });
    // 从几时结束业务字典
    this.getEnumDicts("VACATION_TIME").then((response) => {
      this.thruDateHourOptions = response.data;
    });
    // 审批结论业务字典
    this.getEnumDicts("APPROVAL_RESULT").then((response) => {
      this.approvalResultOptions = response.data;
    });
    // 流程状态业务字典
    this.getEnumDicts("FLOW_STATUS").then((response) => {
      this.flowStatusOptions = response.data;
    });
    // 审批人业务字典
    this.getEntityDicts("SysUser:userId:nickName").then((response) => {
      this.handlerOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 人员字典转换
    personFormat(row, column) {
      let val = null;
      this.personOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.name;
        }
      });
      return val;
    },
    // 审批人字典转换
    approverFormat(row, column) {
      let val = null;
      this.personOptions.forEach((item, index) => {
        if (row.approver === item.partyId) {
          val = item.name;
        }
      });
      return val;
    },
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    vacationTypeIdFormat(row, column) {
      let val = null;
      this.vacationTypeIdOptions.forEach((item, index) => {
        if (row.vacationTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    fromDateHourFormat(row, column) {
      let val = null;
      this.fromDateHourOptions.forEach((item, index) => {
        if (row.fromDateHour === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    thruDateHourFormat(row, column) {
      let val = null;
      this.thruDateHourOptions.forEach((item, index) => {
        if (row.thruDateHour === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    approvalResultFormat(row, column) {
      let val = null;
      this.approvalResultOptions.forEach((item, index) => {
        if (row.approvalResult === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    flowStatusFormat(row, column) {
      let val = null;
      this.flowStatusOptions.forEach((item, index) => {
        if (row.flowStatus === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    handlerFormat(row, column) {
      let val = null;
      this.handlerOptions.forEach((item, index) => {
        if (row.handler === item.userId) {
          val = item.nickName;
        }
      });
      return val;
    },
    /** 查询请假记录列表 */
    getList() {
      this.loading = true;
      listVacation_record(this.queryParams).then((response) => {
        this.vacation_recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vacationRecordId: null,
        partyId: null,
        applyDate: null,
        vacationTypeId: null,
        fromdate: null,
        fromDateHour: null,
        thrudate: null,
        thruDateHour: null,
        totalWorkTime: null,
        description: null,
        urgentContact: null,
        tempPartyId: null,
        marriedDate: null,
        childrenDate: null,
        deathDate: null,
        deathRelative: null,
        inCompanyFlg: null,
        statusId: null,
        affixFilePath: null,
        attendanceRuleId: null,
        relationid: null,
        approvalResult: "0",
        approvalComments: null,
        flowInstId: null,
        flowStatus: "0",
        handler: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.vacationRecordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      (this.editFlag = true), (this.updateFlag = false);
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加请假记录";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      (this.editFlag = false), (this.updateFlag = false);
      this.detailFlag = true;
      this.reset();
      const vacationRecordId = row.vacationRecordId || this.ids;
      getVacation_record(vacationRecordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "请假记录详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const vacationRecordId = row.vacationRecordId || this.ids;
      getVacation_record(vacationRecordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.vacationRecordId != null && this.updateFlag) {
            updateVacation_record(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVacation_record(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vacationRecordIds = row.vacationRecordId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除请假记录编号为"' + vacationRecordIds + '"的数据项？'
        )
        .then(function () {
          return delVacation_record(vacationRecordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ehr/vacation_record/export",
        {
          ...this.queryParams,
        },
        `vacation_record_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
